<?php

// Security measures
if (!defined("PUBLIC_HTML")) {
	
	exit;
	
}

class String {
	
	/**
	 * Encode a string for several purposes
	 * 
	 * @access public
	 * @param string $string	The given string
	 * @param string $type		How to encode the string
	 * @return string $string	Returns modified string
	 * 
	 */
	public function secure ($string, $type) 
	{	
		switch ($type) 
		{	
			case 'password'			: $string = md5(sha1($string)); break;
			case 'sql_single_field'	: $string = htmlspecialchars(addslashes($string)); break;
			case 'url'				: $string = preg_replace("/&nbsp;/", "_", $string); break;
		}
		
		return $string;	
	}
	
	/**
	 * Parses web friendly UBB codes into HTML codes
	 * 
	 * @access public
	 * @param string $bericht	The given string
	 * @return string $bericht	Returns the modified string
	 */
	public function ubb ($bericht) {
		
		$bericht = stripslashes($bericht);
		$bericht = nl2br("$bericht");
		$bericht = eregi_replace("([a-zA-Z0-9_-]+)@([a-zA-Z0-9\._-]+)(\.[a-zA-Z]+)", "<strong><a class=\"review_tekst_link\" href=\"mailto:\\1@\\2\\3\">\\1@\\2\\3</a></strong>", $bericht);
		$bericht = str_replace("[img]","<img src=\"http://",$bericht);
		$bericht = str_replace("[/img]","\">",$bericht);
		$bericht = eregi_replace("\[url\]www.([^\[]*)","<strong><a class=\"review_tekst_link\" href=\"http://www.\\1\" target=_blank>\\1", $bericht);
		$bericht = eregi_replace("\[url\]([^\[]*)","<strong><a class=\"review_tekst_link\" href=\"\\1\" target=_blank>\\1", $bericht);
		$bericht = eregi_replace("(\[url=)([A-Za-z0-9_~&=;\?:%@#./\-]+[A-Za-z0-9/])(\])", "<strong><a class=\"review_tekst_link\" href=\"http://\\2\" target=_blank>", $bericht);
		$bericht = eregi_replace("\[url](http://(.*))", "<strong><a class=\"review_tekst_link\" href=\"\\1\" target=\"_blank\">\\1", $bericht);
		$bericht = eregi_replace("(\[/url\])", "</a></strong>", $bericht);
		$bericht = str_replace("http://http://", "http://", $bericht);
		
		$bericht = str_replace("[b]","<b>",$bericht);
		$bericht = str_replace("[/b]","</b>",$bericht);
		$bericht = str_replace("[i]","<i>",$bericht);
		$bericht = str_replace("[/i]","</i>",$bericht);
		$bericht = str_replace("[u]","<u>",$bericht);
		$bericht = str_replace("[/u]","</u>",$bericht);
		$bericht = str_replace("[*]","<li>",$bericht);
		
		$bericht = str_replace("[center]","<div align=\"center\">",$bericht);
		$bericht = str_replace("[/center]","</div>",$bericht);
		$bericht = str_replace("[left]","<div align=\"left\">",$bericht);
		$bericht = str_replace("[/left]","</div>",$bericht);
		$bericht = str_replace("[right]","<div align=\"right\">",$bericht);
		$bericht = str_replace("[/right]","</div>",$bericht);
		
		$bericht = preg_replace("#\[screen width=(.*?) height=(.*?) id=(.*?)\]#si","<a href=\"http://www.mediainside.nl/games/screenshot.php?screen=\\3\" target=\"_blank\"><img style=\"border: 1px solid #FFFFFF;\" width=\"\\1\" height=\"\\2\" align=\"center\" src=\"http://www.mediainside.nl/images/screenshots/\\3.jpg\"></a>",$bericht);
		$bericht = preg_replace("#\[screenright width=(.*?) height=(.*?) id=(.*?)\]#si","<a href=\"http://www.mediainside.nl/games/screenshot.php?screen=\\3\" target=\"_blank\"><img style=\"border: 1px solid #FFFFFF; margin: 3px;\" width=\"\\1\" height=\"\\2\" align=\"right\" src=\"http://www.mediainside.nl/images/screenshots/\\3.jpg\"></a>",$bericht);
		$bericht = preg_replace("#\[screenleft width=(.*?) height=(.*?) id=(.*?)\]#si","<a href=\"http://www.mediainside.nl/games/screenshot.php?screen=\\3\" target=\"_blank\"><img style=\"border: 1px solid #FFFFFF; margin: 3px;\" width=\"\\1\" height=\"\\2\" align=\"left\" src=\"http://www.mediainside.nl/images/screenshots/\\3.jpg\"></a>",$bericht);
		
		$bericht = preg_replace("#\[dump width=(.*?) height=(.*?) id=(.*?)\]#si","<a href=\"http://www.mediainside.nl/images/dumpmap/\\3.jpg\" target=\"_blank\"><img style=\"border: 1px solid #FFFFFF;\" width=\"\\1\" height=\"\\2\" align=\"center\" src=\"http://www.mediainside.nl/images/dumpmap/\\3.jpg\"></a>",$bericht);
	
		
	 	$bericht = preg_replace('_\[code\](.*?)\[/code\]_ise', "kleuren(' \\1 ')", $bericht);
	
		$bericht = str_replace("[quote]","<table border=\"0\" width=\"90%\"><tr><td width=\"10\">&nbsp;</td><td><font size=\"1\">Quote:</font></td></tr><tr><td width=\"10\">&nbsp;</td><td><hr color=\"#999999\">",$bericht);
		$bericht = str_replace("[/quote]","<hr color=\"#999999\"></td></tr></table>",$bericht);
	
		return $bericht;
	
	}
	
	/**
	 * Cleans given values from general illegal characters for use in databases and url friendly requests
	 * 
	 * @access public
	 * @param string $value	The string that needs to be cleaned up
	 * @return string $value Returns the cleaned up string
	 */
	public function clean ($value)
	{
		$value = trim($value);
		$value = preg_replace('/\s/', '-', $value);
		$value = preg_replace('/[^A-Za-z0-9\-]/', '', $value);
		$value = strtolower($value);
		
		return $value;
	}
	
	public function text ($value)
	{
		//return nl2br($value);
		return $value;
	}
	
}

?>